<template>
  <div class="myforum">
    <fan-hui></fan-hui>
    <div class="nrong"
         v-for="(item,index) in forums"
         :key="index"
         @click="xiang(item.forum_id)">
      <div class="title">
        <p class="tou">{{ item.title }}</p>
        <p class="shan"
           @click.stop="shanchu(item.forum_id)">删除</p>
      </div>
      <div class="zuozhe">
        <div class="img">
          <img :src="item.head_img"
               alt="">
          <div class="name">
            {{ item.username }}
          </div>
        </div>
        <div class="time">
          发布时间：
          {{ item.publish_time }}
        </div>
      </div>
      <div class="content">
        <span>
          {{ item.content }}
        </span>
      </div>
    </div>
    <el-dialog v-model="centerDialogVisible"
               width="500"
               title="是否确定删除该文章？"
               center
               :align-center='true'>
      <span>

      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary"
                     @click="queding()">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <div class="kong"
         v-if="kong===''">
      <div class="fabu"
           @click="fabu()">点我去发布</div>
      <img src="/imgs/kong.jpg"
           alt="">
    </div>
  </div>
</template>



<script>
import { onMounted, ref } from 'vue'
import axios from '@/axios'
import { ElLoading, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import FanHui from '@/components/daohan/FanHui.vue'
export default {
  components: {
    FanHui
  },
  setup() {
    const router = useRouter()
    // 论坛列表
    let forums = ref('')
    // 跳转详情页
    let xiang = (id) => {
      localStorage.setItem('forum_id', id)
      router.push('/forum/details')
    }
    // 弹窗
    let centerDialogVisible = ref(false)
    //判断是否为空
    let kong = ref('')
    //要删除的论坛id
    let shan_id = ref('')
    // 删除
    let shanchu = (id) => {
      centerDialogVisible.value = true
      shan_id.value = id
    }
    //点我去发布
    let fabu = () => {
      router.replace('/forum')
    }
    let queding = () => {
      axios
        .post('/ltshan', { id: shan_id.value })
        .then(() => {
          // console.log(d)
          const loading = ElLoading.service({
            lock: true,
            text: '文章删除中...',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          setTimeout(() => {
            loading.close()
            ElMessage({
              message: '文章删除成功',
              type: 'success'
            })
            //获取我的论坛列表
            axios
              .get('/forums')
              .then((d) => {
                //过滤数据
                forums.value = d.filter((item) => {
                  return item.username == localStorage.getItem('username')
                })
                if (forums.value.length !== 0) {
                  kong.value = '1'
                } else {
                  kong.value = ''
                }
              })
              .catch((err) => console.log(err))
            shan_id.value = ''
            centerDialogVisible.value = false
          }, 2000)
        })
        .catch((err) => console.log(err))
    }
    onMounted(() => {
      //获取我的论坛列表
      axios
        .get('/forums')
        .then((d) => {
          //过滤数据
          forums.value = d.filter((item) => {
            return item.username == localStorage.getItem('username')
          })
          if (forums.value.length !== 0) {
            kong.value = '1'
          } else {
            kong.value = ''
          }
        })
        .catch((err) => console.log(err))
    })

    return {
      forums,
      shanchu,
      centerDialogVisible,
      queding,
      kong,
      fabu,
      xiang
    }
  }
}
</script>

<style scoped lang="less">
.myforum {
  width: 1200px;
  height: 100%;
  margin: 0 auto;
  padding: 20px 0 50px;
  .nrong {
    width: 1000px;
    margin: 0 auto;
    margin-top: 20px;
    border: #cdbf62 2px solid;
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    .title {
      width: 100%;
      height: 50px;
      padding: 0 20px;
      line-height: 50px;
      font-size: 20px;
      font-weight: 600;
      color: #000;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .tou {
        width: 800px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .shan {
        font-size: 16px;
        color: #666;
        cursor: pointer;
      }
    }
    .zuozhe {
      width: 100%;
      height: 45px;
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: #cdbf62 2px dashed;
      .img {
        width: 500px;
        height: 45px;
        display: flex;
        align-items: center;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          margin-right: 10px;
        }
        .name {
          font-size: 18px;
          color: #666;
        }
      }
    }
    .content {
      width: 100%;
      font-size: 16px;
      background-color: #fcfcfa;
      padding: 20px;
      span {
        text-indent: 2em;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3; /* 限制显示的行数 */
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
  .kong {
    width: 1000px;
    margin: 50px auto 0;
    text-align: center;
    position: relative;
    img {
      height: 700px;
    }
    .fabu {
      position: absolute;
      bottom: 40px;
      right: 190px;
      width: 200px;
      height: 40px;
      font-size: 24px;
      font-weight: 600;
      text-align: center;
      line-height: 40px;
      border-radius: 15px;
      background-color: #eeecde;
      border-bottom: #cdbf62 2px solid;
      border-left: #cdbf62 2px solid;
      border-right: #cdbf62 2px dashed;
      border-top: #cdbf62 2px dashed;
      margin: 10px auto 0;
      cursor: pointer;
    }
  }
}
</style>

